<template>
  <div class="controller-test">
    <h1>Nodejs Serial方法</h1>
    <div class="serial-port">测试串口</div>

    <hr />
    <button @click="getPorts">获取串口信息</button>
    <button @click="openPort">打开连接</button>
    <button @click="closePort">断开连接</button>
    <button @click="writePort">输入内容</button>
    <hr />
    <div class="operation-record">
      <div v-for="(log, index) in operation_record_list" :key="index">
        {{ log.time }} ==> {{ log.content }}
      </div>
    </div>
    <hr />
    <button @click="rtcModel">RTC模块</button>
    <button @click="adcModel">ADC模块</button>
    <button @click="frequencyModel">频率模块</button>
    <button @click="diModel">DI模块</button>
    <button @click="doModel">DO模块</button>
    <button @click="eepromModel">EEPROM模块</button>
    <button @click="letModel">LED、KEY、屏幕</button>
    <button @click="cshModel">写入初始化参数</button>
    <button @click="closeSourceModel">关闭源</button>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import dayjs from "dayjs";
import axios from "axios";

// import { scanSerialPorts } from "../methods/connectSerialPort";

const operation_record_list = ref([]);
// 添加日志
const adddLog = (e) => {
  let data = {
    time: dayjs().format("YYYY-MM-DD HH:mm:ss:SSS"),
    content: e,
  };
  operation_record_list.value.push(data);
};

// 生成报文的方法

// nodejs连接串口
const getPorts = () => {
  axios
    .get("http://localhost:3000/controller/modbus-serial")
    .then((res) => {
      console.log(res.data.ports);
    })
    .catch((error) => {
      console.log(error);
    });
};

const openPort = () => {
  axios
    .post("http://localhost:3000/controller/modbus-serial/open", {
      path: "COM7",
    })
    .then((res) => {
      console.log(res.data);
      num.value = 1;
    })
    .catch((error) => {
      console.log(error);
    });
};

const closePort = async () => {
  axios
    .get("http://localhost:3000/controller/modbus-serial/close")
    .then((res) => {
      console.log(res.data);
    })
    .catch((error) => {
      console.log(error);
    });
};
// 初始化
const num = ref(1);
const writePort = () => {
  if (num.value <= 4) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/write", {
        params: {
          num: num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          num.value++;
          if (num.value <= 4) {
            writePort();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
// RTC模块
const rtc_num = ref(1);
const rtcModel = () => {
  if (rtc_num.value <= 5) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/rtc-model", {
        params: {
          num: rtc_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        // rtc_num.value++;
        // if (rtc_num.value <= 5) {
        //   rtcModel();
        // }
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          rtc_num.value++;
          if (rtc_num.value <= 5) {
            rtcModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
// ADC模块
const adc_num = ref(1);
const adcModel = () => {
  if (adc_num.value <= 4) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/adc-model", {
        params: {
          num: adc_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          adc_num.value++;
          if (adc_num.value <= 4) {
            adcModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
// 频率模块
const frequency_num = ref(1);
const frequencyModel = () => {
  if (frequency_num.value <= 4) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/frequency-model", {
        params: {
          num: frequency_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          frequency_num.value++;
          if (frequency_num.value <= 4) {
            frequencyModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
// DI模块
const di_num = ref(1);
const diModel = () => {
  if (di_num.value <= 4) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/di-model", {
        params: {
          num: di_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          di_num.value++;
          if (di_num.value <= 4) {
            diModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
// DO模块
const do_num = ref(1);
const doModel = () => {
  if (do_num.value <= 4) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/do-model", {
        params: {
          num: do_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          do_num.value++;
          if (do_num.value <= 4) {
            doModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
// EEPROM模块
const eeprom_num = ref(1);
const eepromModel = () => {
  if (eeprom_num.value <= 4) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/eeprom-model", {
        params: {
          num: eeprom_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          eeprom_num.value++;
          if (eeprom_num.value <= 4) {
            eepromModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
// 8、LED、KEY、屏幕
const letModel = () => {
  axios
    .get("http://localhost:3000/controller/modbus-serial/led-model")
    .then((res) => {
      console.log(res.data);
      if (res.data.code === 200) {
        adddLog(res.data.data.hexStr.result);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
// 写入初始化参数
const csh_num = ref(1);
const cshModel = () => {
  if (csh_num.value <= 5) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/csh-model", {
        params: {
          num: csh_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          csh_num.value++;
          if (csh_num.value <= 5) {
            cshModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};

// 关闭源
const closeSource_num = ref(1);
const closeSourceModel = () => {
  if (closeSource_num.value <= 2) {
    axios
      .get("http://localhost:3000/controller/modbus-serial/closeSource-model", {
        params: {
          num: closeSource_num.value,
        },
      })
      .then((res) => {
        console.log(res.data);
        if (res.data.code === 200) {
          adddLog(res.data.data.hexStr.result);
          closeSource_num.value++;
          if (closeSource_num.value <= 2) {
            closeSourceModel();
          }
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
};
</script>

<style lang="less" scoped>
</style>